package com.test;

import com.rabbitmq.client.*;
import org.springframework.amqp.core.MessageBuilder;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class RabbitmqUtils {

    private static final String HOST = "192.168.159.142";
    private static final int PORT = 5672;
    private static final String USERNAME = "guest";
    private static final String PASSWORD = "guest";

    // 提供公共的静态方法来获取私有字段的值
    public static String getHost() {
        return HOST;
    }

    public static int getPort() {
        return PORT;
    }

    public static String getUsername() {
        return USERNAME;
    }

    public static String getPassword() {
        return PASSWORD;
    }

    // 提供公共方法来获取和设置factory
    public static ConnectionFactory getFactory() {
        return factory;
    }

    public static void setFactory(ConnectionFactory factory) {
        RabbitmqUtils.factory = factory;
    }

    // 静态变量factory保持私有
    private static ConnectionFactory factory;

    static {
        factory = new ConnectionFactory();
        factory.setHost(getHost());
        factory.setPort(getPort());
        factory.setUsername(getUsername());
        factory.setPassword(getPassword());
    }

    public static Channel createChannelAndSetup(
            String exchangeName,
            BuiltinExchangeType exchangeType,
            String queueName,
            String routingKey) throws Exception {
        // 使用静态factory创建连接和通道
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            channel.exchangeDeclare(exchangeName, exchangeType);
            channel.queueDeclare(queueName, true, false, false, null);
            channel.queueBind(queueName, exchangeName, routingKey);

            return channel;
        }
    }

    private static Channel getChannel() throws Exception {
        try (Connection connection = factory.newConnection()) {
            return connection.createChannel();
        }
    }

    public static void send(String exchange, String routingKey, String message) throws IOException, TimeoutException {
        try (Channel channel = factory.newConnection().createChannel()) {
            channel.exchangeDeclare(exchange, BuiltinExchangeType.FANOUT);
            channel.basicPublish(exchange, routingKey, null, message.getBytes());
        }
    }

    // 简化后的receive方法
    public static void receive(ConnectionFactory factory, String queueName, DeliverCallback deliverCallback, CancelCallback cancelCallback) throws IOException, TimeoutException {
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            channel.queueDeclare(queueName, true, false, false, null); // 声明队列
            channel.basicConsume(queueName, false, deliverCallback, cancelCallback); // 接收消息
        } // try-with-resources 会自动关闭 connection 和 channel
    }


    private static void close(Channel channel) {
        if (channel != null && channel.isOpen()) {
            try {
                channel.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

//package com.test;
//
//import com.rabbitmq.client.*;
//
//import java.util.Map;
//
//public class RabbitmqUtils {
//    private static ConnectionFactory factory = new ConnectionFactory();
//    private static String message;
//
//    static {
//        factory.setHost("192.168.159.142");
//        factory.setPort(5672);
//        factory.setUsername("guest");
//        factory.setPassword("guest");
//    }
//
//    private static Channel getChannel() throws Exception {
//        Connection connection = factory.newConnection();
//        Channel channel = connection.createChannel();
//        return channel;
//    }
//
//    public static void send(String queue, boolean duration, boolean exclusive,
//                            boolean autoDelete, Map<String, Object> args,
//                            String message) {
//        Channel channel = null;
//        try {
//            channel = getChannel();
//            channel.queueDeclare(queue, duration, exclusive, autoDelete, args);
//            channel.basicPublish("", queue, null, message.getBytes());
//        }catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            RabbitmqUtils.close(channel);
//        }
//    }
//
//    public static void receive(String queue, boolean duration, boolean exclusive,
//                               boolean autoDelete, Map<String, Object> args,
//                               DeliverCallback deliverCallback) {
//        Channel channel = null;
//        try {
//            channel = getChannel();
//            channel.queueDeclare(queue, duration, exclusive, autoDelete, args);
//            channel.basicConsume(queue, true, deliverCallback, consumerTag -> {});
//        }catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            RabbitmqUtils.close(channel);
//        }
//    }
//
//    public static void receiveByWQ(String queue, boolean duration, boolean exclusive,
//                                   boolean autoDelete, Map<String, Object> args,
//                                   DeliverCallback deliverCallback, CancelCallback cancelCallback) {
//        Channel channel = null;
//        try {
//            channel = getChannel();
//            channel.queueDeclare(queue, duration, exclusive, autoDelete, args);
//            channel.basicConsume(queue, true, deliverCallback, cancelCallback);
//        }catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            RabbitmqUtils.close(channel);
//        }
//    }
//
//    private static void close(Channel channel) {
//        try {
//            if (channel != null) {
//                Connection connection = channel.getConnection();
//                channel.close();
//                connection.close();
//            }
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
